﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL
Imports Boogaloo.Admin.UI.Controls

Namespace UI.UserControls


    Partial Public Class Site_ContentPlaceHolderManagerItem
        Inherits ContentPlaceHolderManagerBase


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If String.IsNullOrEmpty(cph) Then
                Me.Visible = False
            Else
               
                If Not Page.IsPostBack Then
                    ViewState("load_editor") = False
                    ViewState("editor_index") = -1
                End If

                BindControl()

            End If

        End Sub

        Private Sub BindControl()

            If obj_node.managed_on_publish Then publish_id = Groove.publish_admin_working.publish_id

            If Not String.Equals(cph, orphaned_control_title) Then
                nodecontrols = NodeControl.GetNodeControlsByContentPlaceHolder(obj_node.node_id, publish_id, cph)
            Else
                nodecontrols = NodeControl.GetOrphanedNodeControlsByNodeID(obj_node.node_id, publish_id, contentplaceholder_list)
            End If
            rpt_controls.DataSource = nodecontrols
            rpt_controls.DataBind()

        End Sub


#Region "repeaters"



        Protected Sub rpt_controls_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_controls.ItemDataBound

            Dim nc As NodeControl = CType(e.Item.DataItem, NodeControl)
            If nc IsNot Nothing Then

                Dim li As HtmlGenericControl = CType(e.Item.FindControl("li"), HtmlGenericControl)
                li.Attributes.Add("class", "contentControl")


                Dim lb_call_editor As LinkButton = CType(e.Item.FindControl("lb_call_editor"), LinkButton)
                lb_call_editor.CommandArgument = e.Item.ItemIndex
                lb_call_editor.Text = nc.name

                If publish_id > 0 Then
                    ' this is the working publish check if this control needs updating to publish
                    If nc.ControlSettingDictionary.ContainsKey("publish") Then

                        If Not CType(nc.ControlSettingDictionary("publish").value, Boolean) Then lb_call_editor.Text &= " <span class=""review"">(Missing publish information)</span>"

                    End If

                End If

                lb_call_editor.ToolTip = "Click to open"


                Dim hyp_close_editor As HyperLink = CType(e.Item.FindControl("hyp_close_editor"), HyperLink)
                hyp_close_editor.NavigateUrl = Request.Url.AbsoluteUri
                hyp_close_editor.Text = nc.name
                hyp_close_editor.Visible = False
                hyp_close_editor.ToolTip = "Click to close"

                Dim lb_sortup As LinkButton = CType(e.Item.FindControl("lb_sortup"), LinkButton)
                lb_sortup.CommandArgument = nc.node_control_id
                lb_sortup.CommandName = "sortup"
                If e.Item.ItemIndex = 0 Then lb_sortup.Visible = False

                Dim lb_sortdown As LinkButton = CType(e.Item.FindControl("lb_sortdown"), LinkButton)
                lb_sortdown.CommandArgument = nc.node_control_id
                lb_sortdown.CommandName = "sortdown"
                If e.Item.ItemIndex >= nodecontrols.Count - 1 Then lb_sortdown.Visible = False

                Dim lb_move As LinkButton = CType(e.Item.FindControl("lb_move"), LinkButton)
                lb_move.CommandArgument = nc.node_control_id

                Dim ddl_cph As DropDownList = CType(e.Item.FindControl("ddl_cph"), DropDownList)

                '-----------------------------------------------------
                ' setup the move between contentplaceholder functions
                '-----------------------------------------------------
                Dim fld_move As HtmlGenericControl = CType(e.Item.FindControl("fld_move"), HtmlGenericControl)
                If (Not GetEventTargetClicked() = lb_move.ClientID _
                    And Not GetEventTargetClicked() = ddl_cph.ClientID) Then
                    fld_move.Visible = False
                Else
                    ddl_cph.DataSource = contentplaceholder_list()
                    ddl_cph.DataBind()
                    ddl_cph.Items.Add(New ListItem(orphaned_control_title, FormsAuthentication.HashPasswordForStoringInConfigFile(orphaned_control_title, "MD5")))
                    ddl_cph.SelectedValue = nc.contentplaceholder

                    fld_move.Visible = True
                End If

                '----------------------------------------------------
                ' delete funtions
                '----------------------------------------------------
                
                Dim dwiz As CommitWizard = CType(e.Item.FindControl("dwiz"), CommitWizard)
                dwiz.CommandArgument = nc.node_control_id

                ' extra information
                CType(e.Item.FindControl("lit_node_control_id"), Literal).Text = nc.node_control_id.ToString
                Dim lbl_register As Label = CType(e.Item.FindControl("lbl_register"), Label)
                Dim lbl_callout As Label = CType(e.Item.FindControl("lbl_callout"), Label)

                Dim filename As String = nc.control.ascx_source
                Dim tagname As String = filename.Substring(filename.LastIndexOf("/") + 1).ToLower.Replace(".ascx", "").ToLower

                Dim register As String = Server.HtmlEncode(String.Format("<%@ register tagprefix=""mfb"" tagname=""{0}"" src=""{1}"" %>", tagname, nc.control.ascx_source))
                lbl_register.Text = register
                
                Dim callout As String = Server.HtmlEncode(String.Format("<mfb:{0} runat=""server"" node_control_id=""{1}"" />", tagname, nc.node_control_id))
                lbl_callout.Text = callout


                '---------------------------------------------------
                ' load the editor if available
                '---------------------------------------------------
                Dim partial_editor_clientid As String = String.Format("{0}_{1}", e.Item.ClientID, "TAGGEDEDITOR")

                Dim div_editor As HtmlGenericControl = CType(e.Item.FindControl("div_editor"), HtmlGenericControl)
                If (CType(ViewState("load_editor"), Boolean) And CType(ViewState("editor_index"), Integer) = e.Item.ItemIndex) _
                    Or GetEventTargetClicked().Contains(partial_editor_clientid) Then

                    hyp_close_editor.Visible = True
                    lb_call_editor.Visible = False
                    dwiz.Enabled = False

                    li.Attributes.Add("class", "contentControl box enriched active")

                    Dim ctl As Object = Nothing
                    If Not String.IsNullOrEmpty(nc.control.ascx_source_editor) Then
                        ctl = Me.LoadControl(nc.control.ascx_source_editor)
                    Else
                        ctl = Me.LoadControl(nc.control.ascx_source)
                    End If
                    Try
                        ctl.node_control_id = nc.node_control_id
                    Catch ex As Exception
                        ' this control does not have a node_control_id
                    End Try
                    ctl.ID = "TAGGEDEDITOR"

                    Dim hyp_close As New HyperLink
                    hyp_close.NavigateUrl = Request.Url.PathAndQuery
                    hyp_close.Text = "Cancel"
                    hyp_close.CssClass = "button enriched cancel"
                    div_editor.Controls.Add(ctl)
                    div_editor.Controls.Add(hyp_close)
                    div_editor.Visible = True
                Else
                    div_editor.Visible = False
                End If

            End If

        End Sub

#End Region

#Region "LinkButtons"

        Protected Sub lb_call_editor_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim index As Integer = CType(CType(sender, LinkButton).CommandArgument, Integer)
            Dim nc As NodeControl = nodecontrols(index)

            If nc IsNot Nothing Then
               
                ViewState("load_editor") = True
                ViewState("editor_index") = index

            End If
            BindControl()
        End Sub


        Protected Sub lb_sort_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim nc As NodeControl = NodeControl.GetNodeControl(CType(lb.CommandArgument, Integer))

            If lb.CommandName = "sortup" Then
                nc.sort_order = nc.sort_order - 1
            Else
                nc.sort_order = nc.sort_order + 1
            End If
            nc.Update()

            BindControl()

        End Sub

        Protected Sub lb_move_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim node_control_id As Integer = CType(lb.CommandArgument, Integer)
            hid_node_control_id.Value = node_control_id
        End Sub

        Protected Sub ddl_cph_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim ddl As DropDownList = CType(sender, DropDownList)
            Dim nc As NodeControl = NodeControl.GetNodeControl(CType(hid_node_control_id.Value, Integer))
            nc.contentplaceholder = ddl.SelectedValue
            nc.Update()
            Groove.RefreshPage()
        End Sub


        Protected Sub dwiz_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim dwiz As CommitWizard = CType(sender, CommitWizard)
            Dim node_control_id As Integer = CType(dwiz.CommandArgument, Integer)

            NodeControl.DeleteNodeControl(node_control_id)
            BindControl()
        End Sub




#End Region



        Private _nodecontrols As List(Of NodeControl) = Nothing
        Public Property nodecontrols() As List(Of NodeControl)
            Get
                Return _nodecontrols
            End Get
            Set(ByVal value As List(Of NodeControl))
                _nodecontrols = value
            End Set
        End Property


        Private _cph As String = String.Empty
        Public Property cph() As String
            Get
                Return _cph
            End Get
            Set(ByVal value As String)
                _cph = value
            End Set
        End Property



    End Class

End Namespace